﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Security;


namespace OnlineBusTicket
{
    public partial class ChangeRole : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // DropDownList Role and UserName
                
                ddlRole.DataSource = Roles.GetAllRoles();
                ddlRole.DataBind();
                ddlUserName.DataSource = Membership.GetAllUsers();
                ddlUserName.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
			//Lay tat ca cac roles ma user dang truc thuoc
			string[] rolesOfUser = Roles.GetRolesForUser(ddlUserName.Text);

			//Remove tung role 
            for (int i = 0; i < rolesOfUser.Length; i++)
			{
				Roles.RemoveUserFromRole(ddlUserName.SelectedValue,rolesOfUser[i]);
			}
			//Add role moi cho user
			Roles.AddUserToRole(ddlUserName.Text, ddlRole.Text);
			//Cach lay current UserName
			string userName = HttpContext.Current.User.Identity.Name;
            
            Response.Redirect("ListAllUserRole.aspx");
        }
    }
}